package com.foreveross.pushsdk.core;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.net.SocketTimeoutException;
import java.util.List;



/**
 * @author Huang Li
 * @version 创建时间：2014年5月8日 下午3:50:32
 * HTTP连接
 */
public class HttpConnection {
	private static final String TAG="HttpConnection";
	
	public static String post(String url,List<BasicNameValuePair> paramss,Context context) {
		try {
			if(!isNetWorkAvilable(context)){
				Log.e(TAG, "No NetWork");
				return null;
			}
			HttpClient client=new DefaultHttpClient();
			HttpParams params=client.getParams();
			HttpConnectionParams.setSoTimeout(params, 3000);
			HttpConnectionParams.setConnectionTimeout(params, 3000);
			HttpPost post=new HttpPost(url);
			 
			post.addHeader("charset", HTTP.UTF_8);	
			post.setEntity(new UrlEncodedFormEntity(paramss,HTTP.UTF_8));
			HttpResponse response=client.execute(post);
			int code=response.getStatusLine().getStatusCode();
			if(code!=200){

				Log.e(TAG, "HTTP CODE: "+code);
				return null;
			}
			String resString=EntityUtils.toString(response.getEntity(),HTTP.UTF_8);
			return resString;
		}  catch (SocketTimeoutException e) {
			// TODO: handle exception
			Log.e(TAG, "Socket Time Out");
			e.printStackTrace();
		}catch (ConnectTimeoutException e) {
			e.printStackTrace();
			Log.e(TAG, "Http Time Out");
		}catch (Exception e) {
			e.printStackTrace();
			Log.e(TAG, "Unkonw Exception: "+e);
		}
		return null;
	}
	
	public static String get(String url,Context context) {
		try {
			if(!isNetWorkAvilable(context)){
				Log.e(TAG, "No NetWork");
				return null;
			}
			HttpClient client=new DefaultHttpClient();
			HttpParams params=client.getParams();
			HttpConnectionParams.setSoTimeout(params, 3000);
			HttpConnectionParams.setConnectionTimeout(params, 3000);
			HttpGet get=new HttpGet(url);

			HttpResponse response=client.execute(get);
			int code=response.getStatusLine().getStatusCode();
			if(code!=200){

				Log.e(TAG, "HTTP CODE: "+code);
				return null;
			}
			String resString=EntityUtils.toString(response.getEntity(),HTTP.UTF_8);
			return resString;
		}  catch (SocketTimeoutException e) {
			// TODO: handle exception
			Log.e(TAG, "Socket Time Out");
			e.printStackTrace();
		}catch (ConnectTimeoutException e) {
			e.printStackTrace();
			Log.e(TAG, "Http Time Out");
		}catch (Exception e) {
			e.printStackTrace();
			Log.e(TAG, "Unkonw Exception: "+e);
		}
		return null;
	}
    public static String put(String url,Context context) {
        try {
            if(!isNetWorkAvilable(context)){
                Log.e(TAG, "No NetWork");
                return null;
            }
            HttpClient client=new DefaultHttpClient();
            HttpParams params=client.getParams();
            HttpConnectionParams.setSoTimeout(params, 3000);
            HttpConnectionParams.setConnectionTimeout(params, 3000);
            HttpPut get=new HttpPut(url);

            HttpResponse response=client.execute(get);
            int code=response.getStatusLine().getStatusCode();
            if(code!=200){

                Log.e(TAG, "HTTP CODE: "+code);
                return null;
            }
            String resString=EntityUtils.toString(response.getEntity(),HTTP.UTF_8);
            return resString;
        }  catch (SocketTimeoutException e) {
            // TODO: handle exception
            Log.e(TAG, "Socket Time Out");
            e.printStackTrace();
        }catch (ConnectTimeoutException e) {
            e.printStackTrace();
            Log.e(TAG, "Http Time Out");
        }catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "Unkonw Exception: "+e);
        }
        return null;
    }
	private static boolean isNetWorkAvilable(Context context) {

		ConnectivityManager conManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = conManager.getActiveNetworkInfo();
		if (networkInfo != null) {
			return networkInfo.isAvailable();
		}
		return false;
	}
}
